package com.heyqing.topic.MinDepth_0111;

import java.util.Objects;

/**
 * ClassName:MinDepth
 * Package:com.heyqing.topic.MinDepth_0111
 * Description:
 *
 * @Date:2024/9/14
 * @Author:Heyqing
 */
public class MinDepth {

    private int minDepth = (int) Math.pow(10, 5);

    public int minDepth(TreeNode root) {
        if (Objects.isNull(root)) return 0;
        achieveFunc(root, 0);
        return minDepth;
    }

    private void achieveFunc(TreeNode root, int currentDepth) {
        if (Objects.isNull(root)) return;
        currentDepth++;
        if (currentDepth > minDepth) return;
        if (Objects.isNull(root.left) && Objects.isNull(root.right)) {
            minDepth = Math.min(currentDepth, minDepth);
        }
        achieveFunc(root.left, currentDepth);
        achieveFunc(root.right, currentDepth);
    }
}
